package com.luyi.sensor.dao.sensor;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luyi.sensor.config.Result;
import com.luyi.sensor.dao.sensorRecord.SensorRecordMapper;
import com.luyi.sensor.entity.Sensor;
import com.luyi.sensor.vo.SensorVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SensorBootMapperImpl结果封装层
 * author: luyi
 * time: 2022/1/7 22:10
 */
@Service
public class SensorBootMapperImpl {
    @Autowired
    SensorBootMapper sensorBootMapper;
    @Autowired
    SensorRecordMapper sensorRecordMapper;

    Logger logger = LoggerFactory.getLogger(getClass());

//    @Override
    public Sensor selectById(Integer id) {
        return null;
    }

//    @Override
    public Sensor findMaxByType(String type) {
        return null;
    }

//    public Result<?> list(){
//        System.out.println("  +++++++++++++++   ");
//        List<Sensor> list = sensorBootMapper.list();
//        System.out.println(list.toString());
//        return Result.success();
//    }
    /**
     * 根据时间分页查询
     * @param pageNum
     * @param pageSize
     * @param time
     * @return
     */
//    public Result<?> searchByTime(Integer pageNum, Integer pageSize, int time){ //显示最近X天的数据，返回json字符串
//
//        QueryWrapper<Sensor> wrapper = new QueryWrapper<>();
//        logger.info("【操作】根据时间分页查询");
//
//        Date date; Calendar cal = Calendar.getInstance();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//
//        date=cal.getTime();String end = sdf.format(date);
//        cal.add(Calendar.HOUR,-time*24);
//        date=cal.getTime();String begin = sdf.format(date);
//
////        //查询最近x天的
////        if (StringUtils.isNotBlank(begin)&&StringUtils.isNotBlank(end)){
////            wrapper.between("article_pubtime", begin, end).select(Sensor.class,i -> !i.getColumn().equals("article_content")); //不查询指定的列
////        }
////        //排序
////        wrapper.orderByDesc("article_pubtime");
////
////        Page<Sensor> articlePage = sensorBootMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
//        return Result.success(articlePage);
//    }
    
    
    /**
     * 新增传感器
     * @return
     */
    public Result<?> addSensor(Sensor sensor) {
        List<Sensor> articleList = new ArrayList<>();

        logger.info("【操作】插入单条传感器数据");
        System.out.println(sensor.toString());
        //生成公司ID
        Snowflake snowflake = IdUtil.getSnowflake(1, 1); //终端号与数据中心号，随时间增大
        sensor.setSensor_id(snowflake.nextId());

        //        sensorBootMapper.addSensor(sensor) ; //从新到旧挨个插入
        logger.info("【成功】批量插入");

        return Result.success();
    }

//    @Override
    public List<Sensor> likeName(String name) {
        return null;
    }

    /**
     * 根据id查询
     * @return
     */
//    public Result<?> searchByID(String company_id) {
//        //根据id查询
//        logger.info("【操作】根据id查询");
//        Sensor sensor = sensorBootMapper.selectById(company_id);
//        return Result.success(sensor);
//    }

    /**
     * 执行传入的SQL语句
     */
    public Result test(String SQL){

//        SQL

        return Result.success("SQl执行成功");
    }

    /**
     * 统计概览
     * 分页查询所有的传感器信息
     * @param pageNum
     * @param pageSize
     */
    public Result<?> getAllSensor(int pageNum, int pageSize){

        PageHelper.startPage(pageNum, pageSize, true);//PageHelper工具类分页
        List<SensorVO> list = sensorBootMapper.getAllSensor();//会自动进行分页查询

        int index = 0;
        //遍历输出结果
        for (SensorVO sensorVO : list) {
            //TODO：关于编号的封装谁做，要不我做，但是每一页肯定不能是新的
            index ++;
            System.out.println(index+":"+sensorVO);//第。。。为
        }
        //TODO： 输出日志

        PageInfo page = new PageInfo<>(list);
        System.out.println(page);
        return Result.success(page);
    }

    public Result<?> getSensorById(long sensorId) {
        Object object = sensorBootMapper.getBySensorId(sensorId);
        return Result.success(object);
    }

//    public Result<?> getSensorRecordById(long sensorId) {
//        Object object = sensorRecordMapper.getSensorRecordById(sensorId);
//        return Result.success(object);
//    }
}
